//
//  CAUserInit.m
//  qiche
//
//  Created by Seimei on 12-11-18.
//  Copyright (c) 2012年 iCar. All rights reserved.
//

#import "CAUserInit.h"
#import "CAObject.h"
#import "CAAppDelegate.h"

@interface CAUserInit ()

@end

@implementation CAUserInit

@synthesize vin_code=_vin_code,car_number=_car_number,car_miles=_car_miles;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationController.title = @"录入信息";
    
    if([[NSUserDefaults standardUserDefaults] objectForKey:@"vin_code"])
    {
        _vin_code.text  = [[NSUserDefaults standardUserDefaults] objectForKey:@"vin_code"];
        _vin_code.enabled = FALSE;
    }
    else
    {
        _vin_code.enabled = TRUE;
    }
    
    if([[NSUserDefaults standardUserDefaults] objectForKey:@"car_number"])
    {
        _car_number.text  = [[NSUserDefaults standardUserDefaults] objectForKey:@"car_number"];
        _car_number.enabled = FALSE;
    }
    else
    {
        _car_number.enabled = TRUE;
    }
    
    
    if([[NSUserDefaults standardUserDefaults] objectForKey:@"car_miles"])
    {
        _car_miles.text  = [[NSUserDefaults standardUserDefaults] objectForKey:@"car_miles"];
        _car_miles.enabled = FALSE;
    }
    else
    {
        _car_miles.enabled = TRUE;
    }
    
    

    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (IBAction)num_submit:(id)sender {
    [[NSUserDefaults standardUserDefaults] setValue:_car_number.text forKey:@"car_number"];
    [sender resignFirstResponder];
}

- (IBAction)mile_submit:(id)sender {
    [[NSUserDefaults standardUserDefaults] setValue:_car_miles.text forKey:@"car_miles"];
    [sender resignFirstResponder];

}

- (IBAction)vin_submit:(id)sender {
    [[NSUserDefaults standardUserDefaults] setValue:_vin_code.text forKey:@"vin_code"];
    [sender resignFirstResponder];

}

- (IBAction)submit:(id)sender {
    
    NSString *vinCodeStr = self.vin_code.text;
    vinCodeStr = @"LSVCD2A45BN236371";
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    [dict setValue:vinCodeStr forKey:@"vinCode"];
    [CNetAgentCenter getCarWithParam:dict OnTarget:self andMethodToExecuting:@selector(carInfoReturn:)];
    [dict release];
    [[LKLoadingCenter defaultCenter] postLoadingWithTitle:@"获取车辆信息..." message:nil ignoreTouch: YES];
}


- (void)set_vin_code:(NSString *)code
{
    self.vin_code.text = code;
}


- (IBAction)miss:(id)sender {
    
    [_car_miles resignFirstResponder];
    [_car_number resignFirstResponder];
    [_vin_code resignFirstResponder];

}
- (void)dealloc {
    [_vin_code release];
    [_car_number release];
    [_car_miles release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setVin_code:nil];
    [self setCar_number:nil];
    [self setCar_miles:nil];
    [self setNum_submit:nil];
    [super viewDidUnload];
}

-(void)carInfoReturn:(id)sender
{
    NSDictionary* dic = (NSDictionary*)sender;
    NSString *err = [dic valueForKey:NETERR];
    if (err && [err length] > 0)
    {
        [[LKLoadingCenter defaultCenter] disposeLoadingView];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
                                                        message:err
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles: nil];
        [alert show];
        [alert release];
        return;
    }
    
     if ([[dic objectForKey:@"vin"] isKindOfClass:[NSDictionary class]]) {
         NSDictionary*vinDic = [dic objectForKey:@"vin"];
         CAVinObject *vin = [[CAVinObject alloc] initWithDict:vinDic];
         [CAAppDelegate globalAppdelegate].carVin = vin;
         [vin release];
     }
    [[LKLoadingCenter defaultCenter] disposeLoadingView];
    [[[UIApplication sharedApplication] delegate] applicationSwitchRoot];
    
}

@end
